<?php
/**
 * @author 	Vu Nhat Lam
 * 			Mail: lamvnd@gmail.com
 * 			Mobile: 0984 52 62 68
 * @version 1.0.0
 */
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'btresetpasskey');
define('CSRF_PROTECTION', true);

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array('user');

// pre-cache templates used by all actions
$globaltemplates = array(
	'USERCP_SHELL',
	'usercp_nav_folderbit'
);

// ########################## REQUIRE BACK-END ###########################
require_once('./global.php');
require_once(DIR . '/includes/functions_user.php');

// #######################################################################
// ######################### START MAIN SCRIPT ###########################
// #######################################################################
if (!($permissions['forumpermissions'] & $vbulletin->bf_ugp_forumpermissions['canview']))
{
	print_no_permission();
}

if (empty($vbulletin->userinfo['userid']))
{
	print_no_permission();
}

if ($_POST['do'] == 'reset')
{
	// Change the passkey of the user
	$db->query_write("
		UPDATE `". TABLE_PREFIX ."user` SET
		`passkey` = '" . fetch_random_string() . "'
		WHERE userid= " . $vbulletin->userinfo['userid']
	);

	eval(print_standard_redirect('bt_passkey_changed', true, true));
}

// set shell template name
$shelltemplatename = 'USERCP_SHELL';
$includecss = array();

// initialise onload event
$onload = '';

// start the navbar
$navbits = array('usercp.php' . $vbulletin->session->vars['sessionurl_q'] => $vbphrase['user_control_panel']);

// draw cp nav bar
construct_usercp_nav('passkey');
$navbits[''] = $vbphrase['bt_reset_passkey'];

$page_templater = vB_Template::create('btresetpasskey');

// make navbar
$navbits = construct_navbits($navbits);
$navbar = render_navbar_template($navbits);

// add any extra clientscripts
$clientscripts = (isset($clientscripts_template) ? $clientscripts_template->render() : '');

if (!$vbulletin->options['storecssasfile'])
{
	$includecss = implode(',', $includecss);
}

// shell template
$templater = vB_Template::create($shelltemplatename);
	$templater->register_page_templates();
	$templater->register('includecss', $includecss);
	$templater->register('cpnav', $cpnav);
	$templater->register('HTML', $page_templater->render());
	$templater->register('navbar', $navbar);
	$templater->register('navclass', $navclass);
	$templater->register('onload', $onload);
	$templater->register('pagetitle', $pagetitle);
	$templater->register('template_hook', $template_hook);
	$templater->register('clientscripts', $clientscripts);
print_output($templater->render());
